package com.webserver.core;

import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.File;

/**
 * @author liyang
 * @version 1.0
 * @package com.webserver.core
 * @file DispatcherServlet.java
 * @description
 * @time 2024-05-15 19:14
 */
//          SpringMvc的内容，用于处理请求的工作
public class DispatcherServlet {

    public void service(HttpServletRequest request, HttpServletResponse response){
        File root = new File(
                ClientHandler.class.getClassLoader().getResource(".").toURI()
        );
        File staticDir = new File(root,"static");
        String path = request.getUri();
        File file = new File(staticDir,path);

//            如果文件不存在或者是一个目录：isFile会直接false
//            此时就发送404对应的状态行
        if(file.isFile()){
            //直接用Http里设置的200，ok
            //只设置一个页面
            response.setContentFile(file);
        }else{
            //没找到：修改默认值
            response.setStatusCode(404);
            response.setStatusReason("Not Found!");
            file = new File(staticDir,"404.html");
            response.setContentFile(file);
        }
    }

}
